import 'package:flutter/material.dart';
import 'package:group_bargain/HelloPageWidget.dart';
import 'package:group_bargain/TestPageWidget.dart';

import 'widgets/app_bar.dart';

class BargainHome extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _BargainHomeWidget();
}

//定义数据元
class _Item {
  String name, activeIcon, normalIcon;

  _Item(this.name, this.activeIcon, this.normalIcon);
}

class _BargainHomeWidget extends State<StatefulWidget> {
  final defaultItemColor = Color.fromARGB(255, 125, 125, 125);
  final itemNames = [
    _Item('推荐', 'assets/images/ic_tab_home_active.png',
        'assets/images/ic_tab_home_normal.png'),
    _Item('我的', 'assets/images/ic_tab_profile_active.png',
        'assets/images/ic_tab_profile_normal.png')
  ];

  List<Widget> pages;
  List<BottomNavigationBarItem> itemList;

  @override
  void initState() {
    super.initState();
    print("initState _BargainHomeWidget");
    if (pages == null) {
      pages = [TestPageWidget(), HelloPageWidget()];
    }
    //配置UI列表
    print("配置UI列表");
    if (itemList == null) {
      itemList = itemNames
          .map((item) => BottomNavigationBarItem(
              icon: Image.asset(
                item.normalIcon,
                width: 30.0,
                height: 30.0,
              ),
              title: Text(
                item.name,
                style: TextStyle(fontSize: 10.0),
              ),
              activeIcon:
                  Image.asset(item.activeIcon, width: 30.0, height: 30.0)))
          .toList();
    }
  }

  int _selectedIndex = 0;

  Widget _getPagesWidget(int index) {
    return Offstage(
      offstage: _selectedIndex != index,
      child: TickerMode(
        enabled: _selectedIndex == index,
        child: pages[index],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: MyAppBar(
          isBack: false,
          actionName: '+',
          onPressed: () {
//            NavigatorUtils.push(context, LoginRouter.smsLoginPage);
          },
          centerTitle: "砍价互帮",
        ),
        body: new Stack(
          children: <Widget>[
            _getPagesWidget(0),
            _getPagesWidget(1),
          ],
        ),
        backgroundColor: Color.fromARGB(255, 248, 248, 248),
        bottomNavigationBar: BottomNavigationBar(
          items: itemList,
          onTap: (int index) {
            setState(() {
              _selectedIndex = index;
            });
          },
          iconSize: 24.0,
          currentIndex: _selectedIndex,
          fixedColor: Color.fromARGB(255, 0, 188, 96),
          type: BottomNavigationBarType.fixed,
        ));
  }
}
